<template>
	<view class="init">
		<div class="center">
			<canvas id="myCanvas" style="width:600px; height:400px; border: 1px solid #ffffff;"></canvas>
			<div @click="login()">登录测试 </div>
		</div>
		<tabBar />
	</view>
</template>

<script>   
	export default {
		data() {
			return {

			};
		},
		mounted() {
			 
		},
		methods: {
			async login() {
				var th = this;
				var invitationCode = this.$store.state.wxUserInfo.invitationCode || '60ad9599d568'
				uni.getUserProfile({
					lang: "zh_CN",
					desc: "获取你的昵称、头像、地区及性别",
					success: (response) => {
						var {
							nickName,
							avatarUrl
						} = response;
						const userInfo = response.userInfo;
						console.log("存下的用户信息");
						uni.setStorageSync("userInfo", { userInfo });
						
						uni.login({
							success(res) {
								var {
									code
								} = res;
								th.ajax("/wx/user/wxLogin", {
									appid: "wx27b778c6bb9469de",
									code,
									nickName,
									avatarUrl,
									invitationCode
								}).then(async (res) => {
									console.log("LOGIN", res);
									console.log("LOGIN", res.data.token);
									uni.setStorageSync('token', 'Bearer ' + res.data.token)
									var res = await th.ajax('/wx/user/getInfo')
									console.log("查询用户", res);  
									th.$store.dispatch("asyncApiUserInfo", res.data);
									uni.setStorageSync('user',JSON.stringify( res.data))
									th.toPage("/pages/home/home")
								});
								// uni.request({ method:'GET',url: `https://api.weixin.qq.com/sns/jscode2session?access_token=56_rRwkgVs8RD42YV6hIt8_3DtzW4y7T_RC6In6h4nDauzfnBqzsF-qZA0V8qcfzurLyp4-fnNVl69l4RTtnLzTcbtr0Y9M-O6Te0dUy6PtdNLb6PZn3OASeVkz7amVVpYaGE-xsRFq2AGt62KsDTBjAGAMCK&js_code=${code}&secret=f5c170155804f4e929326f56008079ba&grant_type=authorization_code&appid=wx27b778c6bb9469de`})					  

							},
						});
						
					},
					fail: () => {
						console.error("您拒绝了请求");
					},
				});
			},
		}
	}
</script>

<style lang="scss">
	.init {
		 padding: 100px 0;
	}
</style>
